package com.keepers.repair.service;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.keepers.repair.R;
import com.keepers.repair.activity.MainNewActivity;
import com.keepers.repair.net.LogUtil;


public class KeeperService extends Service {

    public KeeperBinder mBinder = new KeeperBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.e("_SPI_SERVICE onCreate() executed");
//        String name = getString(R.string.app_name);
//        Intent notificationIntent = new Intent(this, MainNewActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
//                notificationIntent, 0);
//        Notification notification = new NotificationCompat.Builder(this)
//                .setContentTitle(name)
//                .setContentText(ServiceState.NORMAL.getStr())
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setContentIntent(pendingIntent)
//                .setPriority(Notification.PRIORITY_MIN)// 设置该通知优先级
//                .build();
//        notification.flags = Notification.FLAG_ONGOING_EVENT;
//        notification.flags |= Notification.FLAG_NO_CLEAR;
//        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
//        startForeground(0x111, notification);


        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        Intent intent = new Intent(this, MainNewActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setContentTitle("检修应用");// 设置通知的标题
        builder.setContentText("检修应用运行中");// 设置通知的内容
        builder.setOngoing(true);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        if(notificationManager != null){
            notificationManager.notify(0x111, notification);
        }

    }

    public static void startKeeperService(Context context) {
        context = context.getApplicationContext();
        LogUtil.e("_SPI_SERVICE _KeeperService_ startSocketService()-S-");
        context.startService(new Intent(context, KeeperService.class));
        LogUtil.e("_SPI_SERVICE _KeeperService_ startSocketService()-E-");
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        super.onDestroy();
        LogUtil.e("_SPI_SERVICE onDestroy() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.e("_SPI_SERVICE " + "onStartCommand()-S-flags:" + flags + " startId:" + startId);
        LogUtil.e("_SPI_SERVICE " + "onStartCommand()-E-:START_STICKY");
        return START_STICKY;
    }

    public class KeeperBinder extends Binder {

        public void startHeartBeat() {
            LogUtil.e("_SPI_SERVICE " + "startHeartBeat()");
        }
    }

}
